package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    static final StudentService stuService = new StudentService();
    static final Scanner sc = new Scanner(System.in);
    public void start() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1" -> addStudent();
                case "2" -> remove();
                case "3" -> updateStudent();
                case "4" -> findStudent();
                case "5" -> {
                    System.out.println("感谢您使用黑马学生管理系统");
                    return;
                }
                default -> System.out.println("您输入的信息有误");
            }
        }
    }

    private void updateStudent() {
        if (extracted()) return;
        int result;
        String id;
        while (true) {
            System.out.println("请您输入要修改的学生id");
            id = sc.next();
            result = stuService.isExists(id);
            if (result != -1) {
                break;
            } else {
                System.out.println("您要修改的学生id不存在，请重新输入");
            }
        }
        Student student = getStudent(id);
        stuService.updateStudent(result,student);
        System.out.println("修改成功！");
    }

    private void remove() {
        if (extracted()) {return;}
        while (true) {
            System.out.println("请输入要删除的学生id");
            String id = sc.next();
            if (extracted()) return;
            int result = stuService.isExists(id);
            if (result != -1){
                stuService.remove(result);
                System.out.println("删除成功！");
                break;
            } else {
                System.out.println("该id不存在，删除失败！");
            }
        }
    }

    private void findStudent() {
        if (extracted()){return;}
        Student[] students = stuService.findStudent();
        if (students == null) return;
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t生日");
        for (Student s : students) {
            if (s != null){
                System.out.println(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "\t\t\t" + s.getBirthday());
            }
        }
    }

    private void addStudent() {
        // 键盘录入学生信息
        String id;
        while (true) {
            System.out.println("请输入要添加的学生id");
            id = sc.next();
            if (stuService.isExists(id) != -1) {
                System.out.println("学生id已存在");
            } else {
                break;
            }
        }
        Student stu = getStudent(id);
        // 调用stuService中的addStudent方法
        boolean result = stuService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Student getStudent(String id) {
        System.out.println("请输入要添加的学生姓名");
        String name = sc.next();
        System.out.println("请输入要添加的学生年龄");
        String age = sc.next();
        System.out.println("请输入要添加的学生生日");
        String birthday = sc.next();

        // 将学生信息封装成学生对象
        return new Student(id, name, age,birthday);
    }

    private boolean extracted() {
        Student[] students = stuService.findStudent();
        if (students == null) {
            System.out.println("当前暂无学生信息，请添加后重试");
            return true;
        }
        return false;
    }
}
